package com.example.xiaoxueqixiangmu.jiemian;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.xiaoxueqixiangmu.R;

import java.util.ArrayList;

public class ChooseTrainsActivity extends AppCompatActivity {
    private RecyclerView rv;
    private ChooseAdapter chooseAdapter;
    private ArrayList<String> dataArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_trains);
        iniActionBar();
        iniRecyclerView();
    }

    public void iniRecyclerView (){
        rv = this.findViewById(R.id.rv);
        dataArray = new ArrayList<>();
        String [] trains = {"北京","北京西","北京南","成都北","成都东","上海","大连","沈阳","沈阳北","天津","天津西","福州","福州南","福州北"};
        for (int i = 0; i < trains.length; i++) {
            dataArray.add(trains[i]);
        }
        chooseAdapter = new ChooseAdapter(this,dataArray);
        chooseAdapter.setMethodObject(new methodObject());
        rv.setAdapter(chooseAdapter);
        rv.setLayoutManager(new LinearLayoutManager(ChooseTrainsActivity.this));
    }

    public void iniActionBar(){
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayUseLogoEnabled(true);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setTitle("车站选择");
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case android.R.id.home:
                Toast.makeText(this,"finsh",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    public class methodObject implements ChooseAdapter.methodObject{

        @Override
        public void method(View view, int position) {
            Intent intent = getIntent();
            intent.putExtra("city",dataArray.get(position));

            if( intent.getIntExtra("key",0) == 100) {
                setResult(100, intent);
            }else {
                setResult(101,intent);
            }
            ChooseTrainsActivity.this.finish();
        }
    }

}